package com.lyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyun.constants.RedisKeyConstants;
import com.lyun.mapper.MovementLikeMapper;
import com.lyun.pojo.MovementLike;
import com.lyun.service.MovementLikeService;
import com.lyun.util.RedisUtil;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

@DubboService
public class MovementLikeServiceImpl extends ServiceImpl<MovementLikeMapper, MovementLike> implements MovementLikeService {

    @Resource
    private MovementLikeMapper movementLikeMapper;

    @Resource
    private RedisUtil redisUtil;

    private static final Integer HAS = 1;
    private static final Integer DONT = 0;


    /**
     * 如果喜欢则返回1,没喜欢就返回0
     *
     * @param movementId
     * @param selfId
     * @return
     */
    @Override
    public Integer isLiked(Long movementId, Long selfId) {
        String key = RedisKeyConstants.MOVEMENTS_LIKE_PREFIX + movementId;
        boolean isLike = redisUtil.sHasKey(key, String.valueOf(selfId));
        if (isLike) {
            return HAS;
        }

        if (movementLikeMapper.isLiked(movementId, selfId) == DONT) {
            return DONT;
        }

        redisUtil.sSet(key, selfId);
        return HAS;
    }

    /**
     * 如果喜欢则返回1,没喜欢就返回0
     *
     * @param movementId
     * @param selfId
     * @return
     */
    @Override
    public Integer isLoved(Long movementId, Long selfId) {
        String key = RedisKeyConstants.MOVEMENTS_LOVE_PREFIX + movementId;
        boolean isLike = redisUtil.sHasKey(key, String.valueOf(selfId));
        if (isLike) {
            return HAS;
        }

        if (movementLikeMapper.isLoved(movementId, selfId) == DONT) {
            return DONT;
        }

        redisUtil.sSet(key, selfId);
        return HAS;
    }

    @Override
    public int getLikeCount(Long id) {
        return movementLikeMapper.getLikeCountByMovementId(id);
    }

    @Override
    public int getLoveCount(Long id) {
        return movementLikeMapper.getLoveCountByMovementId(id);
    }

    /**
     * 更新或者新增动态点赞信息
     *
     * @param userId
     * @param movementId
     */
    @Override
    public int updateOrSave(Long userId, Long movementId) {
        //只有两种情况 一种是存在这条信息了（点过喜欢）了点赞
        //一种是没没点过喜欢 点赞
        QueryWrapper<MovementLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("movement_id", movementId);
        queryWrapper.eq("user_id", userId);
        MovementLike movementLike = movementLikeMapper.selectOne(queryWrapper);

        MovementLike movementLike1 = new MovementLike();
        movementLike1.setLikeTime(LocalDateTime.now());
        movementLike1.setIsLike(true);
        movementLike1.setMovementId(movementId);
        movementLike1.setUserId(userId);
        //判断是否存在，再看插入或是更新
        if (movementLike == null) {
            movementLikeMapper.insert(movementLike1);
        } else {
            movementLike1.setId(movementLike.getId());
            movementLikeMapper.updateById(movementLike1);
        }
        return movementLikeMapper.getLikeCountByMovementId(movementId);
    }

    /**
     * 更新或者删除动态点赞信息
     *
     * @param userId
     * @param movementId
     * @return
     */
    @Override
    public int deleteOrUpdate(Long userId, Long movementId) {
        //有两种情况  一种是点过赞而且点过喜欢
        //还有一种就是 点过赞 而且没点过喜欢
        QueryWrapper<MovementLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("movement_id", movementId);
        queryWrapper.eq("user_id", userId);
        MovementLike movementLike = movementLikeMapper.selectOne(queryWrapper);
        if (Objects.nonNull(movementLike)) {
            if (movementLike.getIsLove() == 0) {
                movementLikeMapper.deleteById(movementLike.getId());
            } else {
                movementLike.setIsLike(false);
                movementLikeMapper.updateById(movementLike);
            }
        }
        return movementLikeMapper.getLikeCountByMovementId(movementId);
    }
}
